<?php

namespace app\api\controller;

use app\api\model\TieBa;
use app\api\model\TieBaCollect AS CollectModel;
use app\api\validate\Page;

class TieBaCollect extends Base
{
    /**
     * 我的收藏贴
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        // 收藏类型
        $type = CollectModel::TYPE_COLLECT;
        $page = $data['page'] = input('page') ? input('page') : 1;
        $size = $data['size'] = input('size') ? input('size') : $this->pageSize;

        // 检测变量
        $validate = new Page();
        if (!$validate->check($data)) {
            return self::sendResult(102, [], $validate->getError());
        }

        $oauthId = $this->oauthInfo['id'];
        $model = CollectModel::getList($oauthId, $type, $page, $size);
        return self::sendResult(self::$okCode, $model);
    }

    /**
     * 点赞 或者 收藏
     * @param $tieId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create($tieId)
    {
        // 1点赞，2收藏
        $type = input('type') ? input('type') : CollectModel::TYPE_PRAISE;
        if ($tieId <= 0) {
            return self::sendResult(100);
        }
       
        $typeArr = CollectModel::attributes('type');
        if (!isset($typeArr[$type])) {
            return self::sendResult(102, [], '类型错误');
        }

        $tie = TieBa::getOneTie($tieId);
        if (empty($tie)) {
            return self::sendResult(118);
        }
        
        $oauthId = $this->oauthInfo['id'];
        
        $model = CollectModel::getOne($oauthId, $tieId, $type);
        if ($model) {
            return self::sendResult(111, [], '您已经' . $typeArr[$type] . '过了');
        }

        // 添加点赞或者收藏记录
        $res = CollectModel::addOne($oauthId, $tieId, $type);
        if ($res === true) {
            $code = self::$okCode;
            $msg = $typeArr[$type] . '成功';
        } else {
            $code = 112;
            $msg = $typeArr[$type] . '失败';
        }
        return self::sendResult($code, [], $msg);
    }

    /**
     * 取消点赞或者收藏记录
     * @param $tieId
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function delete($tieId)
    {
        // 1点赞，2收藏
        $type = input('type') ? input('type') : CollectModel::TYPE_PRAISE;
        if ($tieId <= 0) {
            return self::sendResult(100);
        }

        $typeArr = CollectModel::attributes('type');
        if (!isset($typeArr[$type])) {
            return self::sendResult(102, [], '类型错误');
        }
        
        $oauthId = $this->oauthInfo['id'];
        
        // 删除点赞或者收藏记录
        CollectModel::deleteOne($oauthId, $tieId, $type);

        return self::sendResult(self::$okCode, [], '取消' . $typeArr[$type] . '成功');
    }
}